#!/usr/bin/env python
# coding:utf-8

import unittest
from sql_rice import sql_parse


class TestSentence(unittest.TestCase):

    def test_fields(self):
        sentence = sql_parse.SqlParser('SELECT rut,nombre FROM clientes')
        self.assertListEqual(['rut', 'nombre'], sentence.fields)

    def test_table(self):
        sentence = sql_parse.SqlParser('SELECT rut, nombre FROM clientes')
        self.assertListEqual(['clientes'], sentence.tables)

    def test_params(self):
        sentence = sql_parse.SqlParser('SELECT rut FROM clientes WHERE' + \
                                      ' rut=:rut')
        self.assertListEqual(['rut'], sentence.params)

    def test_params_unique(self):
        sentence = sql_parse.SqlParser('SELECT rut, nombre FROM clientes ' + \
                                      'WHERE rut=:rut OR (rut=:rut AND ' + \
                                      'nombre=:nombre )')
        assert len(sentence.params) == 2

    def test_make_update(self):
        sentence = sql_parse.SqlParser('SELECT rut, nombre FROM clientes')
        self.assertEqual(sentence.update_query,
                         'UPDATE clientes SET rut=:rut, nombre=:nombre ' +
                         'WHERE rut=:old_rut AND nombre=:old_nombre')

    def test_make_insert(self):
        sentence = sql_parse.SqlParser('SELECT rut, nombre FROM clientes')
        self.assertEqual(sentence.insert_query,
                        'INSERT INTO clientes(rut, nombre)' +
                        ' VALUES(:rut, :nombre)')

    def test_make_delete(self):
        sentence = sql_parse.SqlParser('SELECT rut, nombre FROM clientes')
        self.assertEqual(sentence.delete_query,
                        'DELETE FROM clientes WHERE rut=:rut ' + \
                        'AND nombre=:nombre')

